package com.junior.fixclub.party;

import android.content.ContentResolver;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.junior.fixclub.ParamMaker;

import java.lang.reflect.Member;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class FixBuild {
    public static String TAG = "FixClub";
    public void Fix(XC_LoadPackage.LoadPackageParam loadPackageParam){
        fixAndroidSerial(loadPackageParam);
        fixBaseBand(loadPackageParam);
        fixBuildProp(loadPackageParam);
    }

    public void fixAndroidSerial(XC_LoadPackage.LoadPackageParam loadPackageParam){
        Log.e(TAG,"fixAndroidSerial");
        try{
            Class<?> classBuild = XposedHelpers.findClass("android.os.Build", loadPackageParam.classLoader);
            XposedHelpers.setStaticObjectField(classBuild, "SERIAL", ParamMaker.SERIAL);
        }catch (Exception ex){
            Log.e(TAG," AndroidSerial 错误: " + ex.getMessage());
        }
    }

    public void fixBaseBand(XC_LoadPackage.LoadPackageParam loadPackageParam){
        Log.e(TAG,"fixBaseBand");
        try {
            Class<?> classSysProp = Class.forName("android.os.SystemProperties");
            if(classSysProp!=null) {
                XposedHelpers.findAndHookMethod(classSysProp, "get", String.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        // TODO Auto-generated method stub
                        super.afterHookedMethod(param);
                        String serialno = (String) param.args[0];
                        if (serialno.equals("gsm.version.baseband") || serialno.equals("no message")) {
                            param.setResult(ParamMaker.BASEBAND);
                        }
                    }
                });
            }
            /*
            XposedHelpers.findAndHookMethod("android.os.Build", loadPackageParam.classLoader, "getRadioVersion", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    param.setResult("BaseBand");
                }
            });
             */
        } catch (Exception e) {
            Log.e(TAG," BaseBand 错误: " + e.getMessage());
        }
    }

    public void fixBuildProp(XC_LoadPackage.LoadPackageParam loadPackageParam){
        Log.e(TAG,"fixBuildProp");
        try {
            XposedHelpers.findField(Build.class, "BOARD").set(null,ParamMaker.BOARD);
            XposedHelpers.findField(Build.class, "BRAND").set(null,ParamMaker.BRAND);
            XposedHelpers.findField(Build.class, "CPU_ABI").set(null,ParamMaker.CPU_ABI);
            XposedHelpers.findField(Build.class, "CPU_ABI2").set(null,ParamMaker.CPU_ABI2);
            XposedHelpers.findField(Build.class, "DEVICE").set(null,ParamMaker.DEVICE);
            XposedHelpers.findField(Build.class, "HARDWARE").set(null,ParamMaker.HARDWARE);
            XposedHelpers.findField(Build.class, "MANUFACTURER").set(null,ParamMaker.MANUFACTURER);
            XposedHelpers.findField(Build.class, "MODEL").set(null,ParamMaker.MODEL);
            XposedHelpers.findField(Build.class, "PRODUCT").set(null,ParamMaker.PRODUCT);
            XposedHelpers.findField(Build.class, "TAGS").set(null,ParamMaker.TAGS);  //描述build的标签
            XposedHelpers.findField(Build.class, "TYPE").set(null,ParamMaker.TYPE); //设备版本类型

            XposedHelpers.findField(android.os.Build.VERSION.class, "RELEASE").set(null, ParamMaker.RELEASE_VERSION);
            XposedHelpers.findField(Build.VERSION.class, "SDK_INT").set(null,ParamMaker.SDK_INT);
            XposedHelpers.findField(android.os.Build.VERSION.class, "SDK").set(null,ParamMaker.SDK);
            XposedHelpers.findField(android.os.Build.VERSION.class, "CODENAME").set(null,ParamMaker.CODENAME); //写死就行 这个值为固定

            XposedHelpers.findField(Build.class, "DISPLAY").set(null,ParamMaker.DISPLAY);
            XposedHelpers.findField(Build.class, "FINGERPRINT").set(null,ParamMaker.FINGERPRINT);
            XposedHelpers.findField(Build.class, "ID").set(null,ParamMaker.ID);
            XposedHelpers.findField(Build.class, "BOOTLOADER").set(null,ParamMaker.BOOTLOADER); //主板引导程序
            XposedHelpers.findField(Build.class, "HOST").set(null,ParamMaker.HOST);  // 设备主机地址
            XposedHelpers.findField(Build.VERSION.class, "INCREMENTAL").set(null,ParamMaker.INCREMENTAL); //源码控制版本号
            XposedHelpers.findField(Build.class, "TIME").set(null,ParamMaker.TIME);  // 固件时间build

        } catch (Exception e) {
            Log.e(TAG," BuilProp 错误: " + e.getMessage());
        }

        try {
            XposedHelpers.findAndHookMethod("android.provider.Settings.Secure", loadPackageParam.classLoader, "getString", ContentResolver.class, String.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    if (param.args[1].equals(Settings.Secure.ANDROID_ID)) {
                        param.setResult(ParamMaker.ANDROID_ID);
                    }
                }
            });
        } catch (Exception ex) {
            Log.e(TAG," Android ID 错误: " + ex.getMessage());
        }

        try {
            //android.provider.Settings.System system;
            XposedHelpers.findAndHookMethod("android.provider.Settings.System", loadPackageParam.classLoader, "getString", ContentResolver.class, String.class, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    if (param.args[1].equals(Settings.Secure.ANDROID_ID)) {
                        param.setResult(ParamMaker.ANDROID_ID);
                    }
                }
            });
        } catch (Exception ex) {
            Log.e(TAG," Android ID 错误: " + ex.getMessage());
        }

        try {
            Class<?> cls = Class.forName("android.os.SystemProperties");
            if(cls != null){
                for (Member mem : cls.getDeclaredMethods()) {
                    XposedBridge.hookMethod(mem, new XC_MethodHook() {

                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            // TODO Auto-generated method stub
                            super.beforeHookedMethod(param);
                            // 用户的KEY
                            if (param.args.length > 0 && param.args[0] != null && param.args[0].equals("ro.build.description")) {
                                param.setResult(ParamMaker.DESCRIPTION);
                            }
                        }
                    });
                }
            }
        } catch (ClassNotFoundException e) {
            Log.e(TAG," DESCRIPTION 错误: " + e.getMessage());
        }

    }
}
